package org.hh.netty.utils;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class SysUtils {

    static Rectangle rectangle;
    static Robot robot;
    static Dimension screenSize;

    static{
        try {
            robot = new Robot();
            screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            rectangle = new Rectangle(screenSize);
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取屏幕图像
     * @return
     * @throws AWTException
     */
    public static BufferedImage getScreeImage()  {
        return robot.createScreenCapture(rectangle);
    }

    /**
     * 获取屏幕尺寸对象
     * @return
     */
    public static Dimension getDimension(){
        return screenSize;
    }

    /**
     * 获取当前电脑网关
     * @return  192.168.1.
     * @throws UnknownHostException
     */
    public static String getGateway() throws UnknownHostException {
        String hostAddress = getIP();
        return hostAddress.substring(0, hostAddress.lastIndexOf(".") + 1);
    }

    /**
     *  获取当前电脑IP
     * @return
     * @throws UnknownHostException
     */
    public static String getIP() throws UnknownHostException {
        InetAddress inetAddress = InetAddress.getLocalHost();
        return inetAddress.getHostAddress();
    }
}
